package myclient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 * @author JIAO
 */
public class Connection {
	public PrintWriter writer;
	public BufferedReader reader;
	public HashMap<String,FriendMessage> list = new HashMap<String,FriendMessage>();
	public String name;
	public String pic;
	public String motto;
	public String birthday;
	public String city;
	public String sex;
	Connection(String ip,String sPort){
		try {
			int port = Integer.parseInt(sPort);
			System.out.println("重新利用新的端口建立连接");
			System.out.println("新端口号："+port);
			try {
				Thread.currentThread();
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Socket s = new Socket(ip,port);
			reader = new BufferedReader(new InputStreamReader(s.getInputStream(),"UTF-8"));
			StringTokenizer token = new StringTokenizer(reader.readLine(),",");
			name = token.nextToken();
			pic = token.nextToken();
			motto = token.nextToken();
			sex = token.nextToken();
			birthday = token.nextToken();
//			System.out.println("birthday");
			city = token.nextToken();
			//读取自己的信息  name 和 pic
			String line;
			while(!(line=reader.readLine()).equals("end")){
				StringTokenizer tokens = new StringTokenizer(line,",");
				String num = tokens.nextToken();
				String name = tokens.nextToken();
				String pic = tokens.nextToken();
				String status = tokens.nextToken();
				String motto = tokens.nextToken();
				String birthday = tokens.nextToken();
				String city = tokens.nextToken();
				list.put(num, new FriendMessage(name,pic,status,motto,birthday,city));
			}
			writer = new PrintWriter
			(new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"UTF-8")),true);
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
	
	
}

